<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Home\Controller;
use Think\Controller;
class CenterController extends HomeController {
      public function center(){

        $map = array('id'=>session("userid"));
        $map2 = array('uid'=>session("userid"));
        $order=M('order');
        $orderlist=$order->where($map2)->order('createdate desc')->select();
        $buyer=M("buyers")->where($map)->find();
        $count=count($orderlist);
        $this->assign('userlogo',session('userlogo'));
        $this->assign('count',$count);
        $this->assign('buyer',$buyer);
        $this->display();
    }
    
    public function history(){        
        $map = array('uid'=>session("userid"));
        $order=M('order');
        $orderlist=$order->where($map)->order('createdate desc')->select();  
        //dump($orderlist);
        $goods=M('order_goods')->join('RIGHT JOIN gr_order on gr_order_goods.orderid=gr_order.orderid')->where($map)->select();
        //dump($goods);
        $this->assign('goodslist',$goods);
        $this->assign('historyList',$orderlist);         
        $this->display();
    }   
    
    public function cancelOrder($id){
            $userid=session("userid");
            $orderid=$id;
            if(isset($orderid)){
                $map['orderid'] = $orderid;
                $goodsTable=D('product');
                $res=D('order_goods')->where($map)->select();
                foreach ($res as $value){
                    $data2['id']=$value['pid'];
                    $goodsTable->where($data2)->setInc('inventory',$value['cartnum']);    
                }
                $map['status'] = 1000;
                $orderRes=M('order')->where($map)->find();
                if($orderRes["useamount"]>0){
                    //撤销订单返还余额 start
                    $detail="用户撤销订单".$orderid;
                    $act="incre";
                    R("Activity/amountFlow",array($orderRes["useamount"],$detail,$act));
                    $amountData['id']=$userid;
                    D("buyers")->where(array('id'=>$userid))->setInc('amount',$orderRes["useamount"]);
                    //end
                }
                $data['status']= -1;
                $result= M('order')->where($map)->save($data);   
                if($result){
                    $this->redirect('Center/history');
                }else{
                    $this->redirect('Center/history');
                }            
            }                
    }
}
